import { useState, useEffect } from "react";
import { Form, Select, Alert, Tooltip, message, Button, Col } from "antd";
import { purchaseorderAuditPass, purchaseorderCancelled, purchaseorderForcedComplete } from "../service";
import { CommonModal } from "@/views/components/CommonModal";
const ForceCompletionModal = ({ visible, checkedRows, fetchList, handleCancel, filteredArray }) => {
	const [form] = Form.useForm();
	const [codesArrayNum, setCodesArrayNum] = useState([]);

	useEffect(() => {
		if (visible == "PasseExamine") {
			const codesArray = filteredArray.map(item => item.purchaseOrderNo);
			setCodesArrayNum(codesArray);
		} else if (visible == "Cancellation") {
			const codesArray = filteredArray.map(item => item.purchaseOrderNo);
			setCodesArrayNum(codesArray);
		} else {
			const codesArray = filteredArray.map(item => item.purchaseOrderNo);
			setCodesArrayNum(codesArray);
		}
	}, [visible]);
	const onOk = () => {
		form.validateFields().then(async value => {
			if (visible == "PasseExamine") {
				const { success } = await purchaseorderAuditPass(codesArrayNum);
				if (success) {
					message.success("操作成功");
					onCancel();
					fetchList();
				}
			} else if (visible == "Cancellation") {
				const { success } = await purchaseorderCancelled(codesArrayNum);
				if (success) {
					message.success("操作成功");
					onCancel();
					fetchList();
				}
			} else {
				const { success } = await purchaseorderForcedComplete(codesArrayNum);
				if (success) {
					message.success("操作成功");
					onCancel();
					fetchList();
				}
			}
		});
	};
	const onCancel = () => {
		handleCancel();
		form.resetFields();
	};

	return (
		<CommonModal
			title={{ title: visible == "PasseExamine" ? "审核通过" : visible == "Cancellation" ? "作废" : "强制完成" }}
			className="views-table-dialog"
			visible={true}
			onOk={onOk}
			onCancel={onCancel}
			width="333px"
			minHeight="150px"
		>
			<div>
				本次操作数量：<span style={{ color: "rgba(240, 61, 41, 1)" }}>{codesArrayNum.length}</span>
			</div>
			{visible == "ForceCompletionModal" && <div>强制完成后对应的入库单的状态将更改为已完成 请确认是否确认执行该操作</div>}
			{(visible == "PasseExamine" || visible == "Cancellation") && <div>请确认是否确认执行该操作</div>}
		</CommonModal>
	);
};

export default ForceCompletionModal;
